<template>
  <div class="common-page f-c">
    <div class="search-params f-r a-c j-b">
      <el-form inline>
        <el-form-item label="项目类别：">
          <el-select
            v-model="searchParams.bigCategoryId"
            clearable
            @change="changeCategory"
          >
            <el-option v-for="item in options1" :key="item.id" :value="item.id" :label="item.templateCategory" />
          </el-select>
        </el-form-item>
        <el-form-item label="项目子类别：">
          <el-select v-model="searchParams.smallCategoryId" clearable>
            <el-option v-for="item in options2" :key="item.id" :value="item.id" :label="item.templateCategory" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" round @click="onSearch">筛选</el-button>
          <el-button round @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
      <el-form inline>
        <el-form-item>
          <el-button type="primary" round @click="onAdd">添加</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="table-box f-g">
      <el-table
        v-loading="loading"
        height="100%"
        stripe
        :data="tableData"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.8)"
        @selection-change="onSelect"
      >
        <el-table-column type="index" width="80px" label="序号" align="center" />
        <el-table-column v-for="item in columns" :key="item.prop" :prop="item.prop" :label="item.label" align="center" />
        <el-table-column label="操作" fixed="right" width="160px">
          <template slot-scope="scope">
            <el-button type="text" @click="onEdit(scope.row)">事项配置</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-box">
      <el-pagination
        background
        :current-page="pageNo"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalSize"
        @size-change="changePageSize"
        @current-change="changePageNo"
      />
    </div>
    <ItemConfig ref="ItemAddRef" @get-page="initData" />
    <el-dialog
      title="添加模版"
      :visible.sync="dialogValue"
      width="500px"
      append-to-body
    >
      <el-form ref="formRef" :model="ruleForm" :rules="rules" label-width="120px">
        <el-form-item label="项目类别：">
          <el-select
            v-model="ruleForm.bigCategoryId"
            @change="changeCategory2"
          >
            <el-option v-for="item in options1" :key="item.id" :value="item.id" :label="item.templateCategory" />
          </el-select>
        </el-form-item>
        <el-form-item label="项目子类别：">
          <el-select
            v-model="ruleForm.smallCategoryId"
          >
            <el-option v-for="item in options3" :key="item.id" :value="item.id" :label="item.templateCategory" />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onSave">确 定</el-button>
        <el-button @click="dialogValue = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import crud from '@/mixins/crud'
import ItemConfig from './config'
import { http_project_event_template, http_project_process_template } from '@/api'
export default {
  name: 'Index',
  components: {
    ItemConfig
  },
  mixins: [crud],
  data() {
    return {
      request: http_project_event_template,
      searchParams: {
        bigCategoryId: '',
        smallCategoryId: ''
      },
      options1: [
      ],
      options2: [],
      options3: [],
      columns: [
        { prop: 'bigCategoryName', label: '项目类别' },
        { prop: 'smallCategoryName', label: '项目子类别' }
      ],
      tableData: [],
      ruleForm: {
        bigCategoryId: '',
        smallCategoryId: ''
      },
      rules: {
        bigCategoryId: [{ required: true, message: '不能为空' }],
        smallCategoryId: [{ required: true, message: '不能为空' }]
      }
    }
  },
  created() {
    http_project_process_template.getCategory({ top: 0 }).then(res => {
      this.options1 = res.data
    })
  },
  methods: {
    changeCategory(val) {
      if (val) {
        http_project_process_template.getCategory({ top: 1, parentId: val }).then(res => {
          this.options2 = res.data
        })
      } else {
        this.options2 = []
      }
    },
    onReset() {
      this.searchParams.bigCategoryId = ''
      this.searchParams.smallCategoryId = ''
      this.options2 = []
    },
    onAdd() {
      this.dialogValue = true
    },
    onEdit(item) {
      this.$refs.ItemAddRef.open(item)
    },
    changeCategory2(val) {
      if (val) {
        http_project_process_template.getCategory({ top: 1, parentId: val }).then(res => {
          this.options3 = res.data
        })
      } else {
        this.options3 = []
      }
    },
    onSave() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          http_project_event_template.create({
            ...this.ruleForm
          }).then(res => {
            this.$message.success(res.msg)
            this.dialogValue = false
            this.onSearch()
          })
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
